home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_073 / runbackground / runbackground.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  246 lines

  1. /* 
  2.  
  3. --------------
  4. runbackground.c  
  5. ---------------
  6.  
  7. SUMMARY:  A Workbench Disk can be used to autostart an application
  8.       through the use of the startup script and close the startup CLI.
  9.  
  10.  
  11. Users have commented that it is not possible to start a process going 
  12. from the startup script and then cause the initial CLI to go away.   
  13. Here is the solution to that problem, named appropriately:
  14.  
  15.     RUNBACKGROUND
  16.  
  17. which starts and runs a background task.  This does indeed allow you to
  18. create a startup script that will set up your workbench running any
  19. programs you might wish, removing the initial CLI in the process.
  20.  
  21. Your s/startup-sequence can contain lines such as the following:
  22.  
  23.     RUNBACKGROUND -3 clock
  24.     RUNBACKGROUND utilities/calculator
  25.     RUNBACKGROUND -5 utilities/notepad
  26.  
  27. where RUNBACKGROUND is the command and the second parameter is the filename
  28. which may be preceded by a flag-variable that specifies an optional delay 
  29. time.  The delay can be from 0 to 9, for the number of seconds that 
  30. the startup script should sleep while allowing the background task to 
  31. load and start.  I've put that in to minimize thrashing of the disk as it
  32. tries to load several projects at once.
  33.  
  34.  
  35. LIMITATIONS:
  36.  
  37.     The program that you run cannot require any input from an interactive
  38.     CLI that starts it.    Additionally, you cannot specify any file 
  39.     redirection in the command line since this program provides the
  40.     redirection for you already.  If you need to use redirection for
  41.     your command, you can modify the source code where shown, thus
  42.     allowing the redirection to become one of the parameters passed
  43.     through to your program.
  44.  
  45.     RUNBACKGROUND does pass your command line parameters to the program
  46.     you wish to start, but limits the total length of your command
  47.     string to 227 (255 minus the 28 characters for "RUN >NIL: <NIL: " 
  48.     preceding your own file pathname and ">NIL: < NIL: " following it.)
  49.  
  50.  
  51. LINKING INFORMATION:
  52.  
  53.     (Amiga/Lattice C)    use -v option for pass 2   (lc2 -v filename.q)
  54.             to disable stack checking code installation.
  55.             (stack checking code sometimes is incorrect).
  56.  
  57.     FROM lib:Astartup.obj runbackground.o
  58.     TO runbackground
  59.     LIBRARY lib:amiga.lib, lib:lc.lib
  60.  
  61.     ****************************  NOTE:  ********************************
  62.     If you use Lstartup.obj, it won't let the startup CLI go away. This is
  63.     because the source code for Lstartup.asm either opens its own window 
  64.     or uses an existing CLI window (Open("*",....)), so that it has some 
  65.     guaranteed place to put the output.   Astartup.obj does not do this.
  66.     *********************************************************************
  67.  
  68. Hope this helps.
  69.  
  70.  
  71. robp.
  72. */
  73.  
  74. /* runbackground.c */
  75. /* Author:  Rob Peck.  5/9/86 */
  76.  
  77.  
  78. #include "exec/types.h"
  79. #include "exec/memory.h"
  80. #include "libraries/dosextens.h"
  81.  
  82. extern struct FileHandle *Open();
  83. extern struct FileLock *Lock();
  84.  
  85. main(argc, argv)
  86. int argc;
  87. char *argv[];
  88. {    
  89.     LONG success, delaywillbe;
  90.     UBYTE commandstring[255];
  91.     char *test, *filename;
  92.     LONG fromparm;
  93.     struct FileInfoBlock *fib;
  94.     struct FileHandle *nilfh;    /* NOTE: will hang around until next reset */
  95.     struct FileLock *lock;
  96.     
  97.     fib = NULL;            /* No file info block so far. */
  98.     delaywillbe = 1;
  99.  
  100.     if(argc < 2 )
  101.     {
  102. usage:
  103.     printf("Usage: RUNBACKGROUND [ -<loaddelay>] <name> [<parm(s)>]\n");
  104.     printf("          where optional loaddelay is 0-9,\n");
  105.     printf("          specified in seconds for the CLI\n");
  106.     printf("          to sleep, waiting for task to load\n");
  107.     printf("          (minimizes inter-task disk-thrashing)\n");
  108.     if(fib) FreeMem(fib, sizeof(struct FileInfoBlock));
  109.     exit(0);
  110.     }
  111.  
  112.     /* See if there is a delay parameter present */
  113.  
  114.     test = argv[1];
  115.  
  116.     if(*test++ == '-')
  117.     {
  118.     filename = argv[2];    /* argv[1] is delay so argv[2] is file  */
  119.     fromparm = 3;        /* Copy parms from 3 to end          */
  120.  
  121.     if(*test >= '0' && *test <= '9')
  122.     {
  123.         delaywillbe = 1 + (50 * (*test - '0')); 
  124.     }
  125.     if (argc < 3) goto usage; /* Only a delay, and no filename!! */
  126.  
  127.     argc--;        /* one less parm to copy */
  128.     }
  129.     else
  130.     {
  131.     filename = argv[1];
  132.     fromparm = 2;        /* Copy parms from 2 to end         */
  133.     }
  134.  
  135.     /* Now see if the file exists!  If not, it can crash the background
  136.      * CLI and take the system along with it.
  137.      */
  138.     lock = Lock(filename,ACCESS_READ); 
  139.     if(!lock)
  140.     {
  141.     test = filename;
  142.     if(*test == '?') goto usage;
  143.     else
  144.     {
  145.         printf("%ls: Command not found\n",filename);
  146.         goto usage;
  147.     }
  148.     }
  149.     else
  150.     {
  151.     /* If file exists, it better be a file and not a directory */
  152.  
  153.     /* Unfortunately, it is difficult to tell if it is an executable
  154.      * file.  If not executable, we'll still get blown out of the
  155.      * water, but that is up to the user to do it right!
  156.      */
  157.  
  158.     fib =  (struct FileInfoBlock *)
  159.             AllocMem(sizeof(struct FileInfoBlock),MEMF_CLEAR);
  160.     if(!fib)
  161.     {
  162.         UnLock(lock);
  163.         printf("Ran out of memory!\n");
  164.         exit(0);
  165.     }
  166.     else
  167.     {
  168.         success = Examine(lock,fib);
  169.         if(fib->fib_DirEntryType > 0)     /* its a directory!! */
  170.         {
  171.         printf("%ls is a directory, not a file!\n",filename);
  172.         goto usage;
  173.         }
  174.     }
  175.        FreeMem(fib, sizeof(struct FileInfoBlock));
  176.     UnLock(lock);
  177.     }
  178.  
  179.     nilfh = Open("NIL:",MODE_NEWFILE); /* will always succeed */
  180.  
  181.     strcpy( &commandstring[0], "RUN >NIL: <NIL: " );
  182.     strcat( &commandstring[0], filename);  
  183.  
  184.     /* REMOVE THIS NEXT LINE IF YOU WANT TO INCLUDE REDIRECTION IN
  185.      * THE COMMAND LINE FOR RUNBACKGROUND.   (The line was installed
  186.      * to assure that something like "RUNBACKGROUND date ?" would
  187.      * not crash the system.  "Date ?" is expecting to have an interactive
  188.      * CLI, and unless it is specifically told to direct its output to nil:
  189.      * it causes a crash.  If the next line is removed, and you are careful
  190.      * about putting only NON-interactive commands in the command line,
  191.      * everything should be ok.  Notice that if you do not specify a 
  192.      * non-interactive file handle (named_disk_file or NIL:) for your
  193.      * program, it may still prevent the originating CLI from going away
  194.      * until your program ends.  Also note that specifying two instances 
  195.      * of the same redirection (">somewhere" or "<somewhere") for a 
  196.      * background task crashes.
  197.      */
  198.  
  199.     strcat( &commandstring[0], " >NIL: <NIL: ");  /* stop interactive crash */ 
  200.  
  201.     argc--;
  202.  
  203.     while(--argc > 0)    /* Rebuild parameter string for passing it on */
  204.     {
  205.     strcat( &commandstring[0], " ");    /* add a blank */
  206.     strcat( &commandstring[0], argv[fromparm++]);  
  207.     }
  208.  
  209.     success = Execute( &commandstring[0] , nilfh, nilfh);
  210.  
  211.     /* The full command passed to Execute now looks like this:
  212.      *
  213.      *    "RUN >NIL: <NIL: FILENAME >NIL: <NIL: PARAMETER(s)"
  214.      *
  215.      */
  216.  
  217.     if(success)
  218.     {
  219.     printf("Started %ls as a background task\n",filename);
  220.  
  221.     /* Execute, in this case, returns IMMEDIATELY.  The process
  222.      * that is loading the code that is to be run as a background
  223.      * process is working to get everything in and started.  
  224.      */
  225.     }
  226.     /* Now, to minimize thrashing between tasks, lets put this task to 
  227.      * sleep so that the each task actually gets a chance to load.
  228.      */
  229.     Delay(delaywillbe);
  230. }
  231.  
  232. strcpy( to, from )
  233. register char *to, *from;
  234. {
  235.    do {
  236.        *to++ = *from;
  237.    } while( *from++ );
  238. }
  239. strcat( to, from )
  240. register char *to, *from;
  241. {
  242.     while( *to ) to++;
  243.     strcpy( to, from );
  244. }
  245.  
  246.